﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Linq;

namespace cndp.View
{
    public partial class FormSetJumpcodes : Form
    {
        public int CurrentRound;
        public BindingList<Model.JumpCodesAndStyle> ListOfJumpCodesAndStyles = new BindingList<Model.JumpCodesAndStyle>();
        public Model.JumpAppendix JumpAppendixList = new Model.JumpAppendix();
        public Model.Datacheck dataCheck = new Model.Datacheck();
        public double JumpHeight;
        

        public int TotalAmountOfRounds;

        public FormSetJumpcodes()
        {
            InitializeComponent();
        }

        private void FormSetJumpcodes_Load(object sender, EventArgs e)
        {
            this.KeyPreview = true;  
            SetFormInformation(CurrentRound);
            radioButtonStraight.Select();
        }

        private void buttonNextRound_Click(object sender, EventArgs e)
        {

            if (CheckIfJumpExists())
            {
                if(CheckIfJumpAppearsTwice() == false)
                {
                    SetInformation(CurrentRound);
                    if (CurrentRound != TotalAmountOfRounds)
                    {
                        if (CheckInputValues())
                        {
                            CurrentRound++;
                            SetFormInformation(CurrentRound);
                        }
                    }
                } 
            }

            
        }

        private void buttonPreviousRound_Click(object sender, EventArgs e)
        {
            SetInformation(CurrentRound);
            if (CurrentRound != 1)
            {
                if (CheckInputValues())
                {
                    CurrentRound--;
                    SetFormInformation(CurrentRound);
                }
            }
        }

        private void SetInformation(int currentRound)
        {
            if (CheckInputValues())
            {
                if (ListOfJumpCodesAndStyles.Count == TotalAmountOfRounds)
                {
                    labelRound.Text = CurrentRound.ToString();
                    ListOfJumpCodesAndStyles[currentRound - 1].Jumpcode = Convert.ToInt32(comboBoxJumpcode.Text);
                    if (radioButtonStraight.Checked)
                    {
                        ListOfJumpCodesAndStyles[currentRound - 1].Style = "Straight";
                    }
                    else if (radioButtonFree.Checked)
                    {
                        ListOfJumpCodesAndStyles[currentRound - 1].Style = "Free";
                    }
                    else if (radioButtonTuck.Checked)
                    {
                        ListOfJumpCodesAndStyles[currentRound - 1].Style = "Tuck";
                    }
                    else if (radioButtonPike.Checked)
                    {
                        ListOfJumpCodesAndStyles[currentRound - 1].Style = "Pike";
                    }
                }

                else if (currentRound > ListOfJumpCodesAndStyles.Count)
                {
                    ListOfJumpCodesAndStyles.AddNew();
                    labelRound.Text = CurrentRound.ToString();
                    ListOfJumpCodesAndStyles[ListOfJumpCodesAndStyles.Count - 1].Jumpcode =
                        Convert.ToInt32(comboBoxJumpcode.Text);
                    if (radioButtonStraight.Checked)
                    {
                        ListOfJumpCodesAndStyles[ListOfJumpCodesAndStyles.Count - 1].Style = "Straight";
                    }
                    else if (radioButtonFree.Checked)
                    {
                        ListOfJumpCodesAndStyles[ListOfJumpCodesAndStyles.Count - 1].Style = "Free";
                    }
                    else if (radioButtonTuck.Checked)
                    {
                        ListOfJumpCodesAndStyles[ListOfJumpCodesAndStyles.Count - 1].Style = "Tuck";
                    }
                    else if (radioButtonPike.Checked)
                    {
                        ListOfJumpCodesAndStyles[ListOfJumpCodesAndStyles.Count - 1].Style = "Pike";
                    }
                }
            }
        }

        private void SetFormInformation(int currentRound)
        {
            if (currentRound > ListOfJumpCodesAndStyles.Count)
            {
                labelRound.Text = currentRound.ToString();
                comboBoxJumpcode.Text = "";
                radioButtonStraight.Checked = false;
                radioButtonFree.Checked = false;
                radioButtonTuck.Checked = false;
                radioButtonPike.Checked = false;
            }

            else if ((ListOfJumpCodesAndStyles.Count == TotalAmountOfRounds) || (currentRound <= ListOfJumpCodesAndStyles.Count))
            {
                labelRound.Text = currentRound.ToString();
                comboBoxJumpcode.Text = ListOfJumpCodesAndStyles[currentRound - 1].Jumpcode.ToString();
                if (ListOfJumpCodesAndStyles[currentRound - 1].Style == "Straight")
                {
                    radioButtonStraight.Checked = true;
                }
                else if (ListOfJumpCodesAndStyles[currentRound - 1].Style == "Free")
                {
                    radioButtonFree.Checked = true;
                }
                else if (ListOfJumpCodesAndStyles[currentRound - 1].Style == "Tuck")
                {
                    radioButtonTuck.Checked = true;
                }
                else
                {
                    radioButtonPike.Checked = true;
                }
            }
        }

        private bool CheckInputValues()
        {
            if (comboBoxJumpcode.Text == "")
            {
                return false;
            }
            if (!radioButtonFree.Checked && !radioButtonPike.Checked && !radioButtonTuck.Checked && !radioButtonStraight.Checked)
            {
                return false;
            }
            return true;
        }

        private void buttonDone_Click(object sender, EventArgs e)
        {
            if (CheckIfJumpExists())
            {
                if (CheckIfJumpAppearsTwice() == false)
                {
                    SetInformation(CurrentRound);
                    DialogResult = DialogResult.OK;
                    Close();
                }
                
            }
        }

        private void FormSetJumpcodes_MouseDown(object sender, MouseEventArgs e)
        {
           
        }

        private void FormSetJumpcodes_KeyDown(object sender, KeyEventArgs e)
        {
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.Tab)
            {
                labelinvisible1.Visible = !labelinvisible1.Visible;
                labelinvisible2.Visible = !labelinvisible2.Visible;
                labelinvisible3.Visible = !labelinvisible3.Visible;
                labelinvisible4.Visible = !labelinvisible4.Visible;
                labelinvisibleLeft.Visible = !labelinvisibleLeft.Visible;
                labelinvisibleRight.Visible = !labelinvisibleRight.Visible;
                labelinvisibleEnter.Visible = !labelinvisibleEnter.Visible;
            }
            if (e.KeyCode == Keys.Escape)
                DialogResult = DialogResult.Cancel;
            if (ModifierKeys == Keys.Control & ((e.KeyCode == Keys.NumPad1) || (e.KeyCode == Keys.D1)))
                radioButtonStraight.Checked = !radioButtonStraight.Checked;
            if (ModifierKeys == Keys.Control & ((e.KeyCode == Keys.NumPad2) || (e.KeyCode == Keys.D2)))
                radioButtonPike.Checked = !radioButtonPike.Checked;
            if (ModifierKeys == Keys.Control & ((e.KeyCode == Keys.NumPad3) || (e.KeyCode == Keys.D3)))
                radioButtonTuck.Checked = !radioButtonTuck.Checked;
            if (ModifierKeys == Keys.Control & ((e.KeyCode == Keys.NumPad4) || (e.KeyCode == Keys.D4)))
                radioButtonFree.Checked = !radioButtonFree.Checked;
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.Right)
                buttonNextRound.PerformClick();
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.Left)
                buttonPreviousRound.PerformClick();
            if (e.KeyCode == Keys.Enter)
                buttonDone.PerformClick();
        }
        public bool CheckIfJumpExists()
        {
            string jumpStyle = "straight";
            if (radioButtonStraight.Checked)
            {
                jumpStyle = "straight";
            }
            else if (radioButtonFree.Checked)
            {
                jumpStyle = "free";
            }
            else if (radioButtonTuck.Checked)
            {
                jumpStyle = "tuck";
            }
            else if (radioButtonPike.Checked)
            {
                jumpStyle = "pike";
            }

            if (comboBoxJumpcode.SelectedItem == null)
            {
                MessageBox.Show(@"Måste fylla i hoppkod!");
                return false;
            }
            if (dataCheck.CheckJumpExists(Convert.ToInt32(comboBoxJumpcode.Text), jumpStyle, JumpHeight, JumpAppendixList))
            {
                return true;
            }
            else
            {
                MessageBox.Show(@"Detta hopp existerar inte i denna höjdklass");
                return false;
            }
        }
        public bool CheckIfJumpAppearsTwice()
        {
            string jumpStyle = "Straight";
            if (radioButtonStraight.Checked)
            {
                jumpStyle = "Straight";
            }
            else if (radioButtonFree.Checked)
            {
                jumpStyle = "Free";
            }
            else if (radioButtonTuck.Checked)
            {
                jumpStyle = "Tuck";
            }
            else if (radioButtonPike.Checked)
            {
                jumpStyle = "Pike";
            }
            int jumpId = Convert.ToInt32(comboBoxJumpcode.Text);
            Model.JumpCodesAndStyle j = new Model.JumpCodesAndStyle() { Jumpcode = jumpId, Style = jumpStyle };


            if (ListOfJumpCodesAndStyles.Contains(j) && (CurrentRound - 1 != ListOfJumpCodesAndStyles.IndexOf(j)))
            {
                MessageBox.Show("Samma hopp kan ej göras flera gånger");
                return true;
            }
            return false;
        }
    }
}